package com.atguigu.lease.web.admin.tasks;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author:高尚
 * @data:2025/7/11 18:21
 * @description: 定时类定义定时方法
 */
@Component
public class Mytask {
    @Autowired
    private LeaseAgreementService leaseAgreementService;
    //@Scheduled(cron = "0/10 * * * * *") // 每年每月每周每天每时每分每10秒执行一次
    @Scheduled(cron = "0 0 3 * * *") // 每年每月每周每天半夜三点执行一次 半夜系统压力比较小 用户量少
    //设置定时任务   更新租约状态 租约表中的lease_end_date如果小于当前时间new Date()，说明已到期 否则未到期
    public void checkLeaseStatus(){
        LambdaUpdateWrapper<LeaseAgreement> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.lt(LeaseAgreement::getLeaseEndDate,new Date());
        lambdaUpdateWrapper.set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);
        leaseAgreementService.update(lambdaUpdateWrapper);
    }
}
